﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.DAL;
using Backend.DTO;

namespace Backend.BLL
{
    public class StudyBLL
    {
        private StudyDAL studyDAL = new StudyDAL();


        #region Study

        public StudyDTO GetStudy(int studyId)
        {
            try
            {
                return studyDAL.GetStudy(studyId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<StudyDTO> GetSavedStudiesByUserId(int userId)
        {
            try
            {
                return studyDAL.GetSavedStudiesByUserId(userId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public StudyDTO GetUnsavedStudyByUserId(int userId)
        {
            try
            {
                return studyDAL.GetUnsavedStudyByUserId(userId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int AddStudy(StudyDTO aStudyDTO)
        {
            try
            {
                return studyDAL.AddStudy(aStudyDTO);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateStudy(StudyDTO aStudyDto)
        {
            try
            {
                studyDAL.UpdateStudy(aStudyDto);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool DeleteStudy(int studyId)
        {
            try
            {
                return studyDAL.DeleteStudy(studyId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int ShareStudy(int studyId, int userId, int shareToUserId, string shareMessage)
        {
            try
            {
                StudyDTO isShareable = GetStudy(studyId);

                if (isShareable.SharedBy > 0)
                {
                    throw new Exception("Shared Study can not be shared again.");
                }

                return studyDAL.ShareStudy(studyId, userId, shareToUserId, shareMessage);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion


        #region Inclusions And Parameter

        public List<InclusionOrParameterDTO> GetInclusionsByStudyId(int studyId)
        {
            List<InclusionOrParameterDTO> listOfInclusions = new List<InclusionOrParameterDTO>();

            try
            {
                listOfInclusions = studyDAL.GetInclusionsByStudyId(studyId);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return listOfInclusions;
        }

        public List<InclusionOrParameterDTO> GetSharedInclusions()
        {
            try
            {
                return GetInclusionsByStudyId(-1);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<InclusionOrParameterDTO> GetSharedInclusionsByUserId(int userId)
        {
            try
            {
                return studyDAL.GetSharedInclusionsByUserId(userId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<InclusionOrParameterDTO> GetParametersByStudyId(int studyId)
        {
            List<InclusionOrParameterDTO> listOfParameters = new List<InclusionOrParameterDTO>();

            try
            {
                listOfParameters = studyDAL.GetParametersByStudyId(studyId);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return listOfParameters;
        }

        public List<InclusionOrParameterDTO> GetSharedParameters()
        {
            try
            {
                return GetParametersByStudyId(-1);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<InclusionOrParameterDTO> GetSharedParametersByUserId(int userId)
        {
            try
            {
                return studyDAL.GetSharedParametersByUserId(userId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public InclusionOrParameterDTO GetInclusionOrParameter(int inclusionOrParameterId)
        {
            try
            {
                return studyDAL.GetInclusionOrParameter(inclusionOrParameterId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void AddInclusionOrParameter(InclusionOrParameterDTO aInclusionOrParameterDTO)
        {
            try
            {
                studyDAL.AddInclusionOrParameter(aInclusionOrParameterDTO);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateInclusionOrParameter(InclusionOrParameterDTO aInclusionOrParameterDTO)
        {
            try
            {
                studyDAL.UpdateInclusionOrParameter(aInclusionOrParameterDTO);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteInclusionOrParameter(int inclusionOrParameterId)
        {
            try
            {
                studyDAL.DeleteInclusionOrParameter(inclusionOrParameterId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int ShareInclusionOrParameter(int inclusionOrParameterId, int userId, int shareToUserId)
        {
            try
            {
                InclusionOrParameterDTO isShareable = GetInclusionOrParameter(inclusionOrParameterId);

                if (isShareable.SharedBy > 0)
                {
                    throw new Exception("Shared Inclusion or Parameter can not be shared again.");
                }

                return studyDAL.ShareInclusionOrParameter(inclusionOrParameterId, userId, shareToUserId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion


        #region Outcomes

        public List<OutcomeDTO> GetOutcomesByInclusionOrParameterId(int inclusionOrParameterId)
        {
            try
            {
                return studyDAL.GetOutcomesByInclusionOrParameterId(inclusionOrParameterId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public OutcomeDTO GetOutcome(int outcomeId)
        {
            try
            {
                return studyDAL.GetOutcome(outcomeId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void AddOutcome(OutcomeDTO aOutcomeDTO)
        {
            try
            {
                studyDAL.AddOutcome(aOutcomeDTO);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteOutcome(int outcomeId)
        {
            try
            {
                studyDAL.DeleteOutcome(outcomeId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        } 

        #endregion


        #region Outcome Details

        public OutcomeDetailsDTO GetOutcomeDetails(int id)
        {
            try
            {
                return studyDAL.GetOutcomeDetails(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool UpdateOutcomeDetails(OutcomeDetailsDTO aOutcomeDetailsDTO)
        {
            try
            {
                return studyDAL.UpdateOutcomeDetails(aOutcomeDetailsDTO);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
